package com.turing.user.utils;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;
/**
 * @ClassName DESUtils
 * @Description TODO
 * @Author zhiqiang
 * @Date 2022/9/5 15:56
 */
public class DESUtils {

    /**
     *
     * @ClassName: DES3
     * @Description: TODO(这里用一句话描述这个类的作用)
     */
        /**
         *  定义 加密算法,可用
         */
        private static final String ALGORITHM = "DESede";
        /**
         * 密钥(盐值)
         */
        private final static String SECRETKEY = "12365478Hjhfjasdf8fasdf7";
        /**
         *  向量
         */
        private final static String IV = "01234567";
        /**
         *  加解密统一使用的编码方式
         */
        private final static String ENCODING = "UTF-8";

        /***
         * 3DES加密
         *
         * @Title: encrypt
         * @Description: TODO(这里用一句话描述这个方法的作用)
         * @param @param
         *            source
         * @param @return
         *            设定文件
         * @return String 返回类型
         */
        public static String encrypt(String source) {
            try {
                Key deskey = null;
                DESedeKeySpec spec = new DESedeKeySpec(SECRETKEY.getBytes());
                SecretKeyFactory keyfactory;
                keyfactory = SecretKeyFactory.getInstance(ALGORITHM);
                deskey = keyfactory.generateSecret(spec);
                Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
                IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
                cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
                Base64 base64 = new Base64();
                String result = new String(base64.encode(cipher.doFinal(source.getBytes(ENCODING))), ENCODING);
                //加密结果要先经过浏览器时 （转码）
                //result = URLEncoder.encode(result);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
        }

    public static void main(String[] args) {
        String zhangsan = encrypt("zhangssssan");
        System.out.println(zhangsan);
        String decrypt = decrypt(zhangsan);
        System.out.println(decrypt);
    }

        /***
         * 3DES解密
         *
         * @Title: decrypt
         * @param @param
         *            encryptText
         * @param @return
         *            设定文件
         * @return String 返回类型
         */
        public static String decrypt(String encryptText) {
            Key deskey = null;
            DESedeKeySpec spec;
            try {
                spec = new DESedeKeySpec(SECRETKEY.getBytes());
                SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(ALGORITHM);
                deskey = keyfactory.generateSecret(spec);
                Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
                IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
                cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
                Base64 base64 = new Base64();
                return new String(cipher.doFinal(base64.decode(encryptText)), ENCODING);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "000000";
        }
}
